<?php

namespace plugin\admin\app\service\v1;

use plugin\admin\app\common\Util;
use plugin\admin\app\model\V1UserLink;
use plugin\admin\app\model\V1UserLinkExtend;
use support\exception\BusinessException;

/**
 * 会员推广链接服务类
 **/
class UserLinkService
{

    /**
     * 列表
     *
     * @param array $menus
     * @param array $admin
     **/
    public static function select($admin)
    {
        $config = OptionService::getConfig('logo');
        $data = self::getDefaultUserLinkInfo($admin, $config);
        $list = V1UserLink::where('user_id', $admin['id'])->with(['linkextend'])->get();
        foreach ($list as $key => $value) {
            $list[$key]['link'] = $config['register_url'] . $value['link'];
        }
        $list = array_merge([$data], $list->toArray());
        return $list;
    }

    /**
     * 添加/编辑
     *
     * @param integer $id
     * @param array $params
     * @param array $admin
     **/
    public static function create($id, $params, $admin)
    {
        $extend_data = $params['data'];

        $info = null;
        $link_extend_list = null;
        if ($id > 0) {
            $info = V1UserLink::where('id', $id)->where('user_id', $admin['id'])->first();
            if (!$info) {
                throw new BusinessException('推广链接数据不存在!');
            }
            $link_extend_list = V1UserLinkExtend::where('link_id', $info['id'])->get();
        }

        list($line_list,$line_column) = LineService::select();
        $user_extend = UserExtendService::getUserExtend($line_column,$admin);
        $extend_data = UserExtendService::UserExtendCheck($line_column, $user_extend, $extend_data);
        if (!$extend_data) {
            throw new BusinessException('推广链接权限数据不存在!');
        }
        foreach ($extend_data as $key => $value) {
            unset($extend_data[$key]['rp_show'], $extend_data[$key]['zp_show'], $extend_data[$key]['yp1_show'], $extend_data[$key]['yp3_show'], $extend_data[$key]['yp6_show'], $extend_data[$key]['np_show']);
        }

        $data['remark'] = $params['remark'];
        Util::db()->transaction(function () use ($id, $params, $admin, $info, $link_extend_list, $data, $extend_data) {
            if ($info) {
                doUpdate($data, $info);
                $extend_column = array_column($extend_data, null, 'line_id');
                foreach ($link_extend_list as $link_extend_info) {
                    $extend_info = $extend_column[$link_extend_info['line_id']] ?? null;
                    if ($extend_info) {
                        doUpdate($extend_info, $link_extend_info);
                    }
                }
            } else {
                $data['user_id'] = $admin['id'];
                $data['link'] = '?invitation=' . $admin['username'] . '&link_id=';
                $link_id = doInsert($data, new V1UserLink());
                $link_extend_data = array_map(function ($item) use ($admin, $link_id) {
                    $item['user_id'] = $admin['id'];
                    $item['link_id'] = $link_id;
                    return $item;
                }, $extend_data);
                V1UserLinkExtend::insert($link_extend_data);
                V1UserLink::where('id', $link_id)->update(['link' => $data['link'] . $link_id]);
            }
        });
        return true;
    }

    /**
     * 删除
     *
     * @param integer $id
     * @param array $params
     * @param array $admin
     **/
    public static function delete($id, $params, $admin)
    {
        $info = V1UserLink::where('id', $id)->where('user_id', $admin['id'])->first('id');
        if (!$info) {
            throw new BusinessException('推广链接数据不存在!');
        }
        Util::db()->transaction(function () use ($id, $params, $admin, $info) {
            $info->delete();
            V1UserLinkExtend::where('link_id', $id)->delete();
        });
        return true;
    }


    /**
     * 系统默认推广链接
     *
     * @param array $admin
     * @param array $config
     **/
    public static function getDefaultUserLinkInfo($admin, $config)
    {
        $data = [
            'id' => 0,
            'user_id' => $admin['id'],
            'link' => $config['register_url'] . '?invitation=' . $admin['username'],
            'remark' => '系统默认',
            'linkextend' => null,
        ];
        return $data;
    }

}
